<!DOCTYPE html>
          <head>
        <meta charset="utf-8">
            
            <title>
                「Android」DataBinding给Button设置背景无效 | ttdevs
            </title>
            <meta content="width=device-width, initial-scale=1" name="viewport">
            <meta name="theme-color" content="#4184f3">
            
            
            <link href="/favicon.ico" rel="icon"/>
            

            <link rel="stylesheet" href="/css/highlight.light.css">
            <link rel="stylesheet" href="/css/prism-customize.css">
            <link rel="stylesheet" href="/css/nav-icon.css">
            <link rel="stylesheet" href="/css/waves.min.css">
            <link rel="stylesheet" href="/css/jquery.tocify.css">
            <link rel="stylesheet" href="/css/main.css">
            <link rel="stylesheet" href="/css/nav-indicator.css">
            
  

  
  <!-- 谷歌统计 -->
  <script>
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
    m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
    })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

    ga('create', 'UA-97465173-1', 'auto');
    ga('send', 'pageview');

  </script>
  
            </meta>
        </meta>
    </head>

    <body>
        <header>
            <!-- cover image or sth. -->
        </header>
        <div id="main" class="m-scene">
            
<div class="nav-wrapper">

    <div class="container">
        <nav>
            <div class="logo wave">
                <a href="/" id="logo">
                    ttdevs
                </a>
            </div>
            <div class="nav-toggle-icon" >
                <div class="material-hamburger">
                    <span>
                    </span>
                    <span>
                    </span>
                    <span>
                    </span>
                </div>
            </div>
            <div class="menu-wrapper">
                <div class="nav-indicator">
                </div>
                <ul class="menus">
                    
                     
                        <li>
                            <a class="wave " href="/">
                                首页
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/archives">
                                归档
                            </a>
                        </li>
                     
                        <li>
                            <a class="wave " href="/about">
                                关于
                            </a>
                        </li>
                     
                    
                   
                </ul>
            </div>
        </nav>
    </div>
</div>
            <div class="container content">
                <div class="scene_element scene_element--fadein">
                    <div class="row">
    <div class="main">
        <article>
          
          <header class="post-header with-cover" style="background-image:url('/1970/01/01/[Android]DataBinding给Button设置背景无效/cover.jpg')" >
          
          </header>
          <h1 class="post-title">「Android」DataBinding给Button设置背景无效</h1>

          <section class="post-info">
            <span class="post-date">1970/01/01</span>
            
            <span class="post-category">
                <a class="article-category-link" href="/categories/技术/">技术</a>
            </span>
            
            
            <span class="post-tags">
              <ul class="post-tag-list"><li class="post-tag-list-item"><a class="post-tag-list-link" href="/tags/Android/">Android</a></li></ul>
            </span>
            
          </section>

          <section class="post-content">
            <h2 id="0x00-问题"><a href="/1970/01/01/[Android]DataBinding给Button设置背景无效/#0x00-问题" class="headerlink" title="0x00 问题"></a>0x00 问题</h2><p>通过Data Binding给Button设置背景无效。<br>具体表现为给Button设置不同的背景图片，但是无论怎样背景只会显示不同的颜色，而不是显示期望的图片。</p>
<h2 id="0x01-分析"><a href="/1970/01/01/[Android]DataBinding给Button设置背景无效/#0x01-分析" class="headerlink" title="0x01 分析"></a>0x01 分析</h2><p>由于对Data Binding不是很熟悉，所以很奇怪为什么。不管怎样先打印下这个资源的值，发现在布局文件中打印资源值是有的。根据经验，设置不同的背景图片，背景会显示不同的颜色，那这个值可能被解析成了颜色值。</p>
<h2 id="0x02-解决"><a href="/1970/01/01/[Android]DataBinding给Button设置背景无效/#0x02-解决" class="headerlink" title="0x02 解决"></a>0x02 解决</h2><p>在网上搜了一下（容我忘记了具体网址），需要通过不同的方式来设置背景：<br>之前的：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">Button</span></span></div><div class="line">      <span class="attr">android:layout_width</span>=<span class="string">"200dp"</span></div><div class="line">      <span class="attr">android:layout_height</span>=<span class="string">"200dp"</span></div><div class="line">      <span class="attr">android:gravity</span>=<span class="string">"center"</span></div><div class="line">      <span class="attr">android:onClick</span>=<span class="string">"@&#123;(view) -&gt; handler.onStartClick(view)&#125;"</span></div><div class="line">      <span class="attr">android:text</span>=<span class="string">"@string/button_start"</span></div><div class="line">      <span class="attr">android:textColor</span>=<span class="string">"@color/blueColor"</span></div><div class="line">      <span class="attr">android:textSize</span>=<span class="string">"@dimen/global_font_large"</span></div><div class="line">      <span class="attr">android:background</span>=<span class="string">"@&#123;handler.startButtonBg&#125;"</span></div><div class="line">      <span class="attr">tools:background</span>=<span class="string">"@drawable/btn_main_circle_start"</span> /&gt;</div></pre></td></tr></table></figure>
<p>修改之后：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="tag">&lt;<span class="name">Button</span></span></div><div class="line">      <span class="attr">android:layout_width</span>=<span class="string">"200dp"</span></div><div class="line">      <span class="attr">android:layout_height</span>=<span class="string">"200dp"</span></div><div class="line">      <span class="attr">android:gravity</span>=<span class="string">"center"</span></div><div class="line">      <span class="attr">android:onClick</span>=<span class="string">"@&#123;(view) -&gt; handler.onStartClick(view)&#125;"</span></div><div class="line">      <span class="attr">android:text</span>=<span class="string">"@string/button_start"</span></div><div class="line">      <span class="attr">android:textColor</span>=<span class="string">"@color/blueColor"</span></div><div class="line">      <span class="attr">android:textSize</span>=<span class="string">"@dimen/global_font_large"</span></div><div class="line">      <span class="attr">app:backgroundResource</span>=<span class="string">"@&#123;handler.startButtonBg&#125;"</span></div><div class="line">      <span class="attr">tools:background</span>=<span class="string">"@drawable/btn_main_circle_start"</span> /&gt;</div></pre></td></tr></table></figure>
<p>也就是将之前的 <code>android:background</code> 换成 <code>app:backgroundResource</code></p>
<h2 id="0x03-总结"><a href="/1970/01/01/[Android]DataBinding给Button设置背景无效/#0x03-总结" class="headerlink" title="0x03 总结"></a>0x03 总结</h2><p>没啥总结的，Data Binding 还有很多等待探究。</p>
<p><img src="https://raw.githubusercontent.com/ttdevs/ttdevs.github.io/common/images/logo.png" alt="Create by ttdevs"></p>

          </section>
        </article>
        

       
        <div class="pager">
          
            <a class="post-prev pager-item" href="/1970/01/01/[Android]Android Studio 1.x NDK环境搭建/" >
              <strong class="article-nav-caption">上一篇</strong>
              <p class="post-nav-title">「Android」Android Studio 1.x NDK环境搭建</p>
            </a>
          
          
            <a class="post-next pager-item" href="/1970/01/01/[Android]Android Studio 2.0 NDK开发环境搭建/">
              <strong class="article-nav-caption">下一篇</strong>
              <p class="post-nav-title">「Android」Android Studio 2.0 NDK开发环境搭建</p>
            </a>
          
        </div>
        

         <!-- comments -->
        <div class="comment-section">
  
    


</div>

    </div>
    
    <aside>
        <div id="toc">
        </div>
    </aside>
    
</div>

                </div>
            </div>
        </div>
        <footer class="footer">
    <p>由<a href="http://hexo.io/" target="_blank">Hexo</a>强力驱动，搭载<a href="https://github.com/wayou/hexo-theme-gstyle">gstyle</a>主题</p>
    <p>
        &copy; 2017 ttdevs
    </p>
</footer>
<script src="/lib/jquery.js"></script>
<script src="/lib/waves.js"></script>
<script src="/lib/jquery-ui.js"></script>
<script src="/lib/jquery.tocify.js"></script>
<script src="/js/main.js"></script>

    </body>
</html>
